package com.second.util;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.profile.DefaultProfile;

import java.util.Random;

public class SmsUtils {
    public static SendSmsResponse sendSms(String telNum, String code) throws ClientException {
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", "LTAI5tSZ8Vx7We5PoUAnqrHv", "nUWN4dpSN3g9vqjtbWcazVNU4Absxr");
        /** use STS Token
         DefaultProfile profile = DefaultProfile.getProfile(
         "<your-region-id>",           // The region ID
         "<your-access-key-id>",       // The AccessKey ID of the RAM account
         "<your-access-key-secret>",   // The AccessKey Secret of the RAM account
         "<your-sts-token>");          // STS Token
         **/
        IAcsClient client = new DefaultAcsClient(profile);

        SendSmsRequest request = new SendSmsRequest();
        request.setSignName("阿里云短信测试");
        request.setTemplateCode("SMS_154950909");
        request.setPhoneNumbers(telNum);
        request.setTemplateParam("{\"code\":\"" + code + "\"}");

        SendSmsResponse response = client.getAcsResponse(request);

        return response;

    }

    /**
     * @param count 验证码长度
     * @return
     */
    public static String code(int count) {
        String sources = "1234567890";
        int codesLen = sources.length();
        Random rand = new Random(System.currentTimeMillis());
        StringBuilder code = new StringBuilder(count);
        for (int i = 0; i < count; i++) {
            code.append(sources.charAt(rand.nextInt(codesLen - 1)));
        }
        return code.toString();
    }

}
